<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\User as ModelUser;
use app\api\model\UserAuth;
use app\BaseController;
use think\facade\Db;
use think\App;

class User extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function getinfo()
    {
        $user  = $this->request->users;

        $user['phone'] = desensitize($user['phone'],3,4);

        return json(['code'=>1,'data'=>$user]);
    }

    /**
     * 认证
     */
    public function auth(){
        $data = $this->_vali([
            'name.require'      => '姓名不能为空',
            'front.require'      => '身份证正面不能为空',
            'opposite.require'      => '身份证反面不能为空',
            'idcard.require'      => '身份证号不能为空',
        ],'POST');


        $user = ModelUser::mk()->where('id',$this->request->users['id'])->find();
        if($user['is_auth'] == 1) {
            return json(['code'=>0,'msg'=>'用户已认证']);
        }

        $userauth = UserAuth::mk()->where('uid',$this->request->users['id'])->find();
        if(!$userauth) {
            UserAuth::mk()->insert([
                'uid' => $this->request->users['id'],
                'front' => $data['front'],
                'idcard' => $data['idcard'],
                'name' => $data['name'],
                'opposite' => $data['opposite'],
                'create_time' => time(),
                'update_time' => time(),
            ]);
        } else {
            UserAuth::mk()->where('uid',$this->request->users['id'])->update([
                'uid' => $this->request->users['id'],
                'front' => $data['front'],
                'idcard' => $data['idcard'],
                'name' => $data['name'],
                'opposite' => $data['opposite'],
            ]);
        }

        return json(['code'=>1,'msg'=>'认证审核中']);
    }
}
